package com.ls.fw.web.core.servlet.utils;

import javax.servlet.http.HttpServletRequest;

import com.ls.fw.web.core.bean.FwHttpConstant;

public class AjaxUtils {
	public static boolean isAjaxRequest(HttpServletRequest request) {
		String requestedWith = request.getHeader("X-Requested-With");
		return requestedWith != null ? "XMLHttpRequest".equals(requestedWith) : false;
	}

	public static boolean isAjaxUploadRequest(HttpServletRequest request) {
		return request.getParameter("ajaxUpload") != null;
	}

    /**
     * 是否返回json
     * 
     * @author lisheng
     * @date 2015年12月21日 下午9:45:41
     * @version V1.0
     * @param webRequest
     * @param forceJson 是否强制要求返回json
     * @return
     */
    public static boolean isReturnJson(HttpServletRequest request, boolean forceJson){
    	boolean force = false //经过参数设置是否强制
    			,isJson = false;//是否是json
    	String json = request.getParameter(FwHttpConstant.JSON_FLAG);
    	if("1".equalsIgnoreCase(json) || "true".equalsIgnoreCase(json)){
    		force = true;
    		isJson = true;
    	}else if("0".equalsIgnoreCase(json) || "false".equalsIgnoreCase(json)){
    		force = true;
    		isJson = false;
    	}
    	boolean reJson = false;
    	if(force){
    		if(forceJson || isJson){
    			reJson = true;
        	}
    	}else{
    		if(forceJson || AjaxUtils.isAjaxRequest(request)){
    			reJson = true;
        	}
    	}
    	return  reJson;
    } 
}